package cn.jeeweb.web.modules.equipment.service.impl;

import cn.jeeweb.common.mybatis.mvc.service.impl.CommonServiceImpl;
import cn.jeeweb.common.utils.StringUtils;
import cn.jeeweb.web.modules.equipment.entity.EqsRepair;
import cn.jeeweb.web.modules.equipment.entity.EqsRepairRequistion;
import cn.jeeweb.web.modules.equipment.mapper.EqsRepairRequistionMapper;
import cn.jeeweb.web.modules.equipment.service.IEqsRepairRequistionService;
import cn.jeeweb.web.modules.equipment.service.IEqsRepairService;
import cn.jeeweb.web.modules.sys.entity.CodeTable;
import cn.jeeweb.web.modules.sys.service.ICodeTableService;
import cn.jeeweb.web.utils.UserUtils;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @description: eqs_repair_requistion服务接口
 */
@Transactional
@Service("eqsRepairRequistionService")
public class EqsRepairRequistionServiceImpl extends CommonServiceImpl<EqsRepairRequistionMapper, EqsRepairRequistion> implements IEqsRepairRequistionService {

    @Autowired
    ICodeTableService codeTableService;

    @Autowired
    IEqsRepairService eqsRepairService;

    @Override
    public boolean insert(String json) {
        EqsRepairRequistion eqsRepairRequistion = JSON.parseObject(json, EqsRepairRequistion.class);
        CodeTable codeTable = new CodeTable();
        codeTable.setCode("RPRAUTOCODE");
        eqsRepairRequistion.setCode(codeTableService.createNewCode(codeTable));
        return insert(eqsRepairRequistion);
    }

//    @Override
//    public EqsRepairRequistion insertAndSelect(String json){
//        EqsRepairRequistion eqsRepairRequistion = JSON.parseObject(json, EqsRepairRequistion.class);
//        CodeTable codeTable = new CodeTable();
//        codeTable.setCode("RPRAUTOCODE");
//        eqsRepairRequistion.setCode(codeTableService.createNewCode(codeTable));
//        return super.insertAndSelect(JSON.toJSONString(eqsRepairRequistion));
//    }

    @Override
    public String confirmRepair(String[] ids) {
        String result = "";
        List<EqsRepairRequistion> eqsRepairRequistions = baseMapper.getEqsRepairRequistions(ids);
        if (eqsRepairRequistions != null && eqsRepairRequistions.size() > 0) {
            CodeTable codeTable = new CodeTable();
            codeTable.setCode("RPAUTOCODE");
            for (EqsRepairRequistion eqsRepairRequistion : eqsRepairRequistions) {
                if (StringUtils.isNotBlank(eqsRepairRequistion.getRepairId())) {
                    result = "exist";
                } else {
                    EqsRepair eqsRepair = new EqsRepair();
                    eqsRepair.setCode(codeTableService.createNewCode(codeTable));
                    eqsRepair.setRequisitionId(eqsRepairRequistion.getId());
                    eqsRepair.setReceiverId(UserUtils.getUser().getId());
                    eqsRepair.setReceiveTime(new Date());
                    eqsRepair.setStatus("1");
                    eqsRepair.setCreater(UserUtils.getUser().getRealname());
                    eqsRepair.setCreateDate(new Date());
                    eqsRepair.setCreateBy(UserUtils.getUser().getId());
                    eqsRepairService.insertEqsRepair(eqsRepair);
                    eqsRepairRequistion.setRepairId(eqsRepair.getId());
                    insertOrUpdate(eqsRepairRequistion);
                    result = "success";
                }
            }
        }
        return result;
    }

    public List<EqsRepairRequistion> getEqsRepairRequistions(String[] ids) {
        List<EqsRepairRequistion> eqsRepairRequistions = baseMapper.getEqsRepairRequistions(ids);
        if (eqsRepairRequistions != null && eqsRepairRequistions.size() > 0) {
            CodeTable codeTable = new CodeTable();
            codeTable.setCode("RPAUTOCODE");
            for (EqsRepairRequistion eqsRepairRequistion : eqsRepairRequistions) {
                EqsRepair eqsRepair = new EqsRepair();
                eqsRepair.setCode(codeTableService.createNewCode(codeTable));
                eqsRepair.setRequisitionId(eqsRepairRequistion.getId());
                eqsRepair.setReceiverId(UserUtils.getUser().getId());
                eqsRepair.setReceiveTime(new Date());
                eqsRepair.setStatus("1");
            }
        }
        return eqsRepairRequistions;
    }
}